{layout name="layout" /}
<div class="layui-fluid">
  <div id="table1"></div>
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">
        <!-- 可以放搜索条件 -->
      </div>
    </div>
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-role" data-type="batchdel"> 删除 </button>
        <button class="layui-btn layuiadmin-btn-role" data-type="add"> 添加 </button>
      </div>

      <table id="LAY-user-back-role" lay-filter="LAY-user-back-role"></table>
      <script type="text/html" id="buttonTpl">
        {{#  if(d.check == true){ }}
          <button class="layui-btn layui-btn-xs">已审核</button>
        {{#  } else { }}
          <button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button>
        {{#  } }}
      </script>
      <script type="text/html" id="table-useradmin-admin">
        <a class="layui-btn layui-btn-normal layui-btn-xs {{# if(d.isSys) {}} layui-btn-disabled{{#}}}" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs {{# if(d.isSys) {}} layui-btn-disabled{{#}}}" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.config({
    base: "/static/layuiadmin/" //静态资源所在路径
  }).extend({
    index: "lib/index" //主入口模块
  }).use(["index", "useradmin", "table"], function () {
    var $ = layui.$,
      form = layui.form,
      table = layui.table;
    //搜索角色
    form.on("select(LAY-user-adminrole-type)", function (data) {
      //执行重载
      table.reload("LAY-user-back-role", {
        where: {
          role: data.value
        }
      });
    });

    //事件
    var active = {
      batchdel: function () {
        var checkStatus = table.checkStatus("LAY-user-back-role"),
          checkData = checkStatus.data; //得到选中的数据

        if (checkData.length === 0) {
          return layer.msg("请选择数据");
        }

        layer.confirm("确定删除吗？", function (index) {
          $.post(
            "/admin/role/delete",
            {
              list: $.map(checkData, function (v) {
                return v["id"];
              })
            },
            function (result) {
              if (result.data) {
                table.reload("LAY-user-back-role");
                layer.msg("已删除");
                layer.close(index);
              } else {
                layer.msg(result.msg);
              }
            }
          );
        });
      },
      add: function () {
        layer.open({
          type: 2,
          title: "添加新角色",
          content: "/admin/role/add",
          area: ["440px", "480px"],
          btn: ["确定", "取消"],
          yes: function (index, layero) {
            var iframeWindow = window["layui-layer-iframe" + index],
              submit = layero
                .find("iframe")
                .contents()
                .find("#LAY-user-role-submit");
            //监听提交
            iframeWindow.layui.form.on(
              "submit(LAY-user-role-submit)",
              function (data) {
                var field = data.field; //获取提交的字段
                var treeData = iframeWindow.urTree.getChecked();
                var newArr = [];
                function unid(arr) {
                  for (var i = 0; i < arr.length; i++) {
                    newArr.push(arr[i]["id"]);
                    if (
                      Array.isArray(arr[i]["children"]) ||
                      arr[i]["children"].length > 0
                    ) {
                      unid(arr[i]["children"]);
                    }
                  }
                }
                unid(treeData);
                $.post(
                  "/admin/role/add",
                  {
                    name: field.name,
                    key: field.key,
                    pid: field.pid,
                    permissions: newArr
                  },
                  function (result) {
                    if (result.data) {
                      table.reload("LAY-user-back-role"); //数据刷新
                      layer.close(index); //关闭弹层
                    } else {
                      layer.msg(result.msg);
                    }
                  }
                );
              }
            );
            submit.trigger("click");
          }
        });
      }
    };
    $(".layui-btn.layuiadmin-btn-role").on("click", function () {
      var type = $(this).data("type");
      active[type] ? active[type].call(this) : "";
    });
  });
</script>